//
//  ChangeRoleViewController.m
//  penzer
//
//  Created by caiyc on 2018/10/4.
//  Copyright © 2018年 caiyc. All rights reserved.
//

#import "ChangeRoleViewController.h"
#import "AppDelegate.h"
#import "LoginViewController.h"

@interface ChangeRoleViewController ()
@property (weak, nonatomic) IBOutlet UIView *contentView;
@property (weak, nonatomic) IBOutlet UIButton *user_Btn;
@property (weak, nonatomic) IBOutlet UIButton *host_Btn;
@property (weak, nonatomic) IBOutlet UIButton *bussion_Btn;
@property (weak, nonatomic) IBOutlet UIButton *G_Bussion_Btn;
@property (weak, nonatomic) IBOutlet UILabel *host_Acount;
@property (weak, nonatomic) IBOutlet UILabel *bussion_Acount;
@property (weak, nonatomic) IBOutlet UILabel *G_bussion_Acount;
@property (weak, nonatomic) IBOutlet UILabel *user_Acount;

@end

@implementation ChangeRoleViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
     [self setNavigation:1 andTitle:@"切换身份" andNaviColor:[UIColor whiteColor] andTitleColor:nil];
    _user_Btn.tag = RoleType_YH;
    _host_Btn.tag = RoleType_ZB;
    _bussion_Btn.tag = RoleType_SJ;
    _G_Bussion_Btn.tag = RoleType_GHS;
    
    if([XTool GetDefaultInfo:USER_INFO]){//用户
        NSDictionary *dic = [XTool GetDefaultInfo:USER_INFO];
        NSString *acounts = [XTool setMobileToCharters:dic[@"mobile"]];
        self.user_Acount.text = [NSString stringWithFormat:@"用户 %@",acounts];
      
    }else{
        self.user_Acount.text = @"用户 离线状态";
        [self.user_Btn setTitle:@"登录" forState:UIControlStateNormal];
    }
    
    
    if([XTool GetDefaultInfo:HOST_USER]){//主播
        NSDictionary *dic = [XTool GetDefaultInfo:HOST_USER];
        NSString *acounts = [XTool setMobileToCharters:dic[@"mobile"]];
        self.host_Acount.text = [NSString stringWithFormat:@"主播 %@",acounts];
     //   self.host_Acount.text = dic[@"mobile"];
    }else{
        self.host_Acount.text = @"主播 离线状态";
        [self.host_Btn setTitle:@"登录" forState:UIControlStateNormal];
    }
    
    
    if([XTool GetDefaultInfo:BUSSION_INFO]){//商家
        NSDictionary *dic = [XTool GetDefaultInfo:BUSSION_INFO];
        NSString *acounts = [XTool setMobileToCharters:dic[@"account"]];
        self.bussion_Acount.text = [NSString stringWithFormat:@"商家 %@",acounts];
       // self.bussion_Acount.text = dic[@"mobile"];
    }else{
        self.bussion_Acount.text = @"商家 离线状态";
        [self.bussion_Btn setTitle:@"登录" forState:UIControlStateNormal];
    }
    
    
    if([XTool GetDefaultInfo:G_BUSSION_INFO]){//供货商
        NSDictionary *dic = [XTool GetDefaultInfo:G_BUSSION_INFO];
        NSString *acounts = [XTool setMobileToCharters:dic[@"mobile"]];
        self.G_bussion_Acount.text = [NSString stringWithFormat:@"供货商 %@",acounts];
        // self.bussion_Acount.text = dic[@"mobile"];
    }else{
        self.G_bussion_Acount.text = @"供货商 离线状态";
        [self.G_Bussion_Btn setTitle:@"登录" forState:UIControlStateNormal];
    }
    
}



- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden = YES;
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    self.navigationController.navigationBar.hidden = NO;
}



- (IBAction)changeFunc:(UIButton *)sender
{
    RoleType roleType = sender.tag;
    switch (roleType) {
        case RoleType_YH:{//用户
            if([XTool GetDefaultInfo:USER_INFO]){
                [self changeWithRoleType:RoleType_YH];
            }else{
                [self goComenLoginWithRoleType:RoleType_YH];
            }
            break;
        }
            
        case RoleType_ZB:{//主播
            if([XTool GetDefaultInfo:HOST_USER]){
                [self changeWithRoleType:RoleType_ZB];
            }else{
                [self goComenLoginWithRoleType:RoleType_ZB];
            }
            break;
        }
            
        case RoleType_SJ:{//商家
            if([XTool GetDefaultInfo:BUSSION_INFO]){
                [self changeWithRoleType:RoleType_SJ];
            }else{
                [self goComenLoginWithRoleType:RoleType_SJ];
            }
            break;
        }
            
        case RoleType_GHS:{//供货商
            if([XTool GetDefaultInfo:G_BUSSION_INFO]){
                [self changeWithRoleType:RoleType_GHS];
            }else{
                [self goComenLoginWithRoleType:RoleType_GHS];
            }
            break;
        }
            
    }
}



- (void)changeWithRoleType:(RoleType)roleType
{
    [XTool SaveDefaultInfo:@(roleType) Key:Prole];
    AppDelegate *app =(AppDelegate*) [[UIApplication sharedApplication]delegate];
    [app changeRootWithRoleType:roleType];
    [[NSNotificationCenter defaultCenter] postNotificationName:STOPTIMENOTO object:nil];
}


- (void)goComenLoginWithRoleType:(RoleType)roleType
{
    if(self.islogin){
        !self.selectRole?:self.selectRole(roleType);
        [self.navigationController popViewControllerAnimated:1];
    }else{
        [APPDELEGATE goComenLoginWithRoleType:roleType targetVc:self];
    }
}

@end
